package com;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 类描述：
 *
 * @auther niezhuxe
 * @date 2020/6/12
 */
public class Test1 {

    public static void main(String[] args) throws InterruptedException, ExecutionException{
        /** 测试执行方法*/
        long startTime = System.currentTimeMillis();
        simpleTest();
        long endTime = System.currentTimeMillis();
        System.out.println("当前测试方法1时间："+(endTime-startTime)+"毫秒");

        long startTime2 = System.currentTimeMillis();
        threadTest();
        long endTime2 = System.currentTimeMillis();
        System.out.println("当前测试方法2时间："+(endTime2-startTime2)+"毫秒");
    }

    /** 测试方法-1 采用单线程循环处理*/
    public static void simpleTest(){
        /** 1、获取数据集合*/
        List<String> list = findList();

        /** 2、获取包含index关键字数据集合*/
        Map<String,Object> map = new HashMap<>();
        if(list != null && list.size() >0){
            for(int i=0;i<list.size();i++){
                if(list.get(i).contains("index")){
                    map.put(String.valueOf(i+1),list.get(i));
                }
            }
        }else{
           throw new NullPointerException();
        }
        /** 3、处理数据获取ip信息*/
        Map<String,String> resutl = new HashMap<>();
        map.forEach((k,v) ->{
            String dns = findDns(String.valueOf(v));
            resutl.put(k,findIP(dns));
        });

        /** 4、遍历结果Map*/
//        printMap(resutl);
    }
    
    /** 多线程测试*/

    public static void threadTest() throws InterruptedException, ExecutionException{
        /** 1、获取数据集合*/
        List<String> list = findList();

        ExecutorService pool = Executors.newFixedThreadPool(10);
        Map<String, String> map = new HashMap<>();

        /** 创建任务集合*/
        List<Callable<String>> taskList = new ArrayList<>();
        Callable<String> task = null;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                int key = i;

                task = new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        String url = list.get(key);
                        /** 判断当前url是否饱和index*/
                        if (url.contains("index")) {
                            String dns = findDns(url);
                            /** 获取当前url 序号和ip*/
                            return key + ":" + findIP(dns);
                        }
                        return "0";
                    }
                };
                taskList.add(task);
            }
            /** 批量提交任务，阻塞等待任务完成*/
            List<Future<String>> results = pool.invokeAll(taskList);
            for(Future<String> str:results){
                String ss = str.get();
                if(ss != null && !"0".equals(ss)){
                    int flag = ss.indexOf(":");
                    map.put(ss.substring(0,flag),ss.substring(flag,ss.length()-1));
                }
            }
        }else{
                throw new NullPointerException();
        }

        pool.shutdown();
        /** 2、遍历结果Map*/
//        printMap(map);
    }
    
    /** 生成一个数据集合*/
        public static List<String> findList(){
        List<String> list = new ArrayList<>();
        String url = "https://m.yymh03.com/pages/#!/book/read/1224?chapter=232095";
        String url2 = "http://m.yymh03.com/pages/#!/index/read/1224?chapter=232095";
        String url3 = "https://m.yymh03.com/pages/#!/index/read/1224?chapter=232095";

        for(int i = 0;i<1000;i++){
            if(i%3 == 0){
                list.add(url3);
            }else if(i%2 == 0){
                list.add(url2);
            }else{
                list.add(url);
            }
        }
        return list;
    }

    /** 根据域名获取ip*/
        public static String findIP(String dns) {
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getByName(dns);
        }catch (Exception e){
            e.printStackTrace();
        }
        return inetAddress.getHostAddress();
    }

    /** 根据url获取域名*/
        public static String findDns(String url){

        String sub;
        String dnx;
        if(url != null && url.length() > 0){
            /** 判断url是否是https*/
           if("https".equals(url.substring(0,5))){
               /** 例如：https://www.baidu.com/index/.....*/
               sub = url.substring(8);
               dnx = sub.substring(0,sub.indexOf("/"));
           }else{
               /** 例如：http://www.baidu.com/index/.....*/
               sub = url.substring(7);
               dnx = sub.substring(0,sub.indexOf("/"));
           }
        }else{
            throw new NullPointerException();
        }
        return dnx;
    }

    /** 遍历Map数据*/
        public static void printMap(Map map){
        if(map != null && map.size() > 0){
            map.forEach((k,v)->System.out.println("序号："+k+",IP:"+v));
        }else{
            throw new NullPointerException();
        }
    }
}
